//
//  NewListTableViewController.m
//  CarStop
//
//  Created by lanou3g on 15/7/16.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "NewListTableViewController.h"
#import "NewListTableViewCell.h"
#import "RequestMessageData.h"
#import "NewList.h"

@interface NewListTableViewController ()

@property (nonatomic, retain)NSMutableArray *listArray;

@end

@implementation NewListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    
    self.listArray = [NSMutableArray array];
    
    RequestMessageData *reqNewMesData = [[RequestMessageData alloc] init];
    [reqNewMesData requestMessageUrl:@"http://183.232.160.141/app.api.autohome.com.cn/autov4.8.0/news/newslist-pm2-c0-nt1-p1-s20-l0.json" reqSuccessBlockM:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSDictionary *medianDic = [bigDic objectForKey:@"result"];
            
        for (NSDictionary *smallDic in [medianDic objectForKey:@"newslist"]) {
            NewList *newList = [[NewList alloc] init];
            newList.newsMessage = [smallDic objectForKey:@"title"];
            newList.newsDate = [smallDic objectForKey:@"time"];
            newList.newsReplycount = [NSString stringWithFormat:@"%d",[[smallDic objectForKey:@"replycount"] intValue]];
            newList.newsPhotoUrl = [smallDic objectForKey:@"smallpic"];
            newList.newsJumpID = [smallDic objectForKey:@"id"];
            newList.newsJumpNewHtmlID = [smallDic objectForKey:@"lasttime"];
            [self.listArray addObject:newList];
        }
        [self.tableView reloadData];
    } reqErrorBlockM:^(NSError *error) {
        
    }];
   
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 各种布局cell

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
//    return 20;
    return self.listArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"newsCell";
    NewListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[NewListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    
    NewList *listData = [self.listArray objectAtIndex:indexPath.row];
    cell.nList = listData;
    NSLog(@"调用nList的set方法");
    
    
    [listData addObserver:self forKeyPath:@"newsPhoto" options:NSKeyValueObservingOptionNew context:(__bridge void *)(indexPath)];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 90;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

    NewListTableViewCell *cell = (NewListTableViewCell *)[self.tableView cellForRowAtIndexPath:(__bridge NSIndexPath *)context];
    cell.newsPhotoView.image = [change objectForKey:@"new"];
    [object removeObserver:self forKeyPath:@"newsPhoto"];

}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
